package com.zy.mc_module.wheel.recyclerview;

import androidx.annotation.NonNull;
import androidx.collection.SparseArrayCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created on 18/10/25
 * 封装Adapter 添加头部尾部
 */
public class RecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static int BASE_ITEM_TYPE_HEADER = 100000;
    private static int BASE_ITEM_TYPE_FOOTER = 200000;
    /***头部视图***/
    private SparseArrayCompat<View> mHeaderViews = new SparseArrayCompat<>();
    /***尾部视图***/
    private SparseArrayCompat<View> mFootViews = new SparseArrayCompat<>();
    /***需要包装的Adapter***/
    private RecyclerView.Adapter mInnerAdapter;

    public RecyclerAdapter(RecyclerView.Adapter mInnerAdapter) {
        this.mInnerAdapter = mInnerAdapter;
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeaderViewPos(position)) {
            return mHeaderViews.keyAt(position);
        } else if (isFooterViewPos(position)) {
            return mFootViews.keyAt(position - getHeadersCount() - getRealItemCount());
        }
        return mInnerAdapter.getItemViewType(position - getHeadersCount());
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (null != mHeaderViews.get(viewType)) {
            return BaseViewHolder.createViewHolder(mHeaderViews.get(viewType));
        } else if (null != mFootViews.get(viewType)) {
            return BaseViewHolder.createViewHolder(mFootViews.get(viewType));
        }
        return mInnerAdapter.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        //如果是HeaderView 或者是 FooterView 不进行绑定数据,因为是从外部传进来的,不由列表更新
        if (isHeaderViewPos(position) || isFooterViewPos(position)) {
            return;
        }
        mInnerAdapter.onBindViewHolder(holder, position - getHeadersCount());
    }

    @Override
    public int getItemCount() {
        return getHeadersCount() + getFootersCount() + getRealItemCount();
    }

    /***
     * 判断当前位置是否是头部
     * @param position
     * @return
     */
    private boolean isHeaderViewPos(int position) {
        return position < getHeadersCount();
    }

    /***
     * 判断当前位置是否是尾部
     * @param position
     * @return
     */
    private boolean isFooterViewPos(int position) {
        return position >= getHeadersCount() + getRealItemCount();
    }

    /***
     * 获取头部视图
     * @return
     */
    private int getHeadersCount() {
        return mHeaderViews.size();
    }

    /***
     * 获取尾部视图
     * @return
     */
    private int getFootersCount() {
        return mFootViews.size();
    }

    /***
     * 主视图
     * @return
     */
    private int getRealItemCount() {
        return mInnerAdapter == null ? 0 : mInnerAdapter.getItemCount();
    }

    /***
     * 添加头部
     * @param view
     */
    public void addHeaderView(View view) {
        mHeaderViews.put(mHeaderViews.size() + BASE_ITEM_TYPE_HEADER, view);
    }

    /***
     * 添加尾部
     * @param view
     */
    public void addFootView(View view) {
        mFootViews.put(mFootViews.size() + BASE_ITEM_TYPE_FOOTER, view);
    }

    /***
     * 清除尾部
     */
    public void clearFootView() {
        mFootViews.clear();
    }

    /***
     * 添加尾部
     * @param view
     */
    public void addFootView1(View view) {
        if (null != mInnerAdapter) {
            mInnerAdapter.notifyDataSetChanged();
        }
        mFootViews.put(mFootViews.size() + BASE_ITEM_TYPE_FOOTER, view);
    }

}
